package com.ruoyi.project.system.wxUser.controller;

import java.util.Date;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import com.ruoyi.project.system.weixinUtil.message.resp.RespTextMessage;
import com.ruoyi.project.system.weixinUtil.utils.MessageUtil;
import com.ruoyi.project.system.wxUser.domain.WxUser;
import com.ruoyi.project.system.wxUser.service.IWxUserService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.RegexUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 微信用户信息操作处理
 * 
 * @author zhangshilin
 * @date 2019-08-13
 */
@Controller
@RequestMapping("/system/wxUser")
public class WxUserController extends BaseController
{
    private String prefix = "system/wxUser";
	
	@Autowired
	private IWxUserService wxUserService;
	@Autowired
	private IUserService userService;
	
	@RequiresPermissions("system:wxUser:view")
	@GetMapping()
	public String wxUser()
	{
	    return prefix + "/wxUser";
	}
	
	@PostMapping("/bind")
	@ResponseBody
	public AjaxResult bind(String tel,String openid)
	{
		if("".equals(tel)) {
			return AjaxResult.error("绑定失败");
		}
		if(RegexUtils.isMobile(tel)) {
			WxUser wxUser = wxUserService.selectWxUserByOpenid(openid);
			if(wxUser==null) {
				return AjaxResult.error("未关注公众号，请您先关注公众号");
			}
			if(wxUser.getTel()!=null&&RegexUtils.isMobile(wxUser.getTel())) {
				return AjaxResult.error("当前微信号已绑定过，请先解绑");
			}
			
			User user = userService.selectUserByPhoneNumber(tel);
			if(user==null) {
				return AjaxResult.error("系统内无该手机号,请联系管理员添加您的账号后再尝试绑定");
			}
			wxUser.setTel(tel);
			wxUser.setUserId(user.getUserId().intValue());
			wxUserService.updateWxUser(wxUser);
			
		}else {
			return AjaxResult.error("手机号不正确，请检查后重试");
		}
		return AjaxResult.success("绑定成功,请退出当前页面");
	}
	
	/**
	 * 查询微信用户列表
	 */
	@RequiresPermissions("system:wxUser:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(WxUser wxUser)
	{
		startPage();
        List<WxUser> list = wxUserService.selectWxUserList(wxUser);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出微信用户列表
	 */
	@RequiresPermissions("system:wxUser:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WxUser wxUser)
    {
    	List<WxUser> list = wxUserService.selectWxUserList(wxUser);
        ExcelUtil<WxUser> util = new ExcelUtil<WxUser>(WxUser.class);
        return util.exportExcel(list, "wxUser");
    }
	
	/**
	 * 新增微信用户
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存微信用户
	 */
	@RequiresPermissions("system:wxUser:add")
	@Log(title = "微信用户", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(WxUser wxUser)
	{		
		return toAjax(wxUserService.insertWxUser(wxUser));
	}

	/**
	 * 修改微信用户
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		WxUser wxUser = wxUserService.selectWxUserById(id);
		mmap.put("wxUser", wxUser);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存微信用户
	 */
	@RequiresPermissions("system:wxUser:edit")
	@Log(title = "微信用户", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(WxUser wxUser)
	{		
		return toAjax(wxUserService.updateWxUser(wxUser));
	}
	
	/**
	 * 删除微信用户
	 */
	@RequiresPermissions("system:wxUser:remove")
	@Log(title = "微信用户", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(wxUserService.deleteWxUserByIds(ids));
	}
	
}
